<template>
  <div class="inbound-confirmation">
    <!-- 页面标题 -->
    <div class="page-title">
      <h2>入库确认列表</h2>
    </div>
    
    <!-- 功能按钮区域 -->
    <div class="operation-bar">
      <el-button type="primary" size="default" @click="handleAdd">新增</el-button>
      <el-button type="danger" size="default" @click="handleDelete">删除</el-button>
      <el-button size="default" @click="handleRestore">还原</el-button>
      <el-button size="default" @click="handleImport">导入</el-button>
      <el-button size="default" @click="handleExport">导出</el-button>
      <el-button size="default" @click="handleViewLog">查看日志</el-button>
      <div class="flex-spacer"></div>
      <el-button size="default" @click="handleDesign">设计</el-button>
    </div>

    <!-- 搜索表单区域 -->
    <div class="search-form">
      <el-form :model="searchForm" inline>
        <el-form-item label="确认时间:">
          <el-date-picker
            v-model="searchForm.startDate"
            type="date"
            placeholder="选择日期"
            size="default"
            style="width: 180px;"
          />
          <span class="date-separator">-</span>
          <el-date-picker
            v-model="searchForm.endDate"
            type="date"
            placeholder="选择日期"
            size="default"
            style="width: 180px;"
          />
        </el-form-item>
        <el-form-item label="入库类别:">
          <el-select v-model="searchForm.type" placeholder="全部" style="width: 180px;" size="default">
            <el-option label="全部" value="" />
            <el-option label="成品入库" value="成品入库" />
            <el-option label="还资入库" value="还资入库" />
            <el-option label="拆装入库" value="拆装入库" />
            <el-option label="半成品报废入库" value="半成品报废入库" />
            <el-option label="申请直接入库" value="申请直接入库" />
            <el-option label="库管直接入库" value="库管直接入库" />
            <el-option label="采购入库" value="采购入库" />
          </el-select>
        </el-form-item>
        <el-form-item label="入库编号:">
          <el-input v-model="searchForm.code" placeholder="" size="default" style="width: 180px;" />
        </el-form-item>
        <el-form-item label="入库主题:">
          <el-input v-model="searchForm.subject" placeholder="" size="default" style="width: 180px;" />
        </el-form-item>
        <el-form-item label="库管人员:">
          <el-input v-model="searchForm.manager" placeholder="请选择或输入" size="default" style="width: 180px;">
            <template #suffix>
              <el-icon><search /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="default" @click="handleSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 表格区域 -->
    <div class="table-container">
      <el-table
        :data="tableData"
        style="width: 100%"
        border
        stripe
        highlight-current-row
        @selection-change="handleSelectionChange"
        v-loading="loading"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column label="序号" type="index" width="70" />
        <el-table-column
          prop="subject"
          label="入库确认主题"
          min-width="250"
        >
          <template #default="scope">
            <el-link type="primary" @click="handleViewDetail(scope.row)">{{ scope.row.subject }}</el-link>
          </template>
        </el-table-column>
        <el-table-column
          prop="code"
          label="入库编号"
          width="150"
        >
          <template #default="scope">
            <el-link type="primary" @click="handleViewInboundDetail(scope.row)" v-if="scope.row.code">{{ scope.row.code }}</el-link>
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="relatedApplication"
          label="关联入库申请单"
          min-width="200"
        >
          <template #default="scope">
            <el-link type="primary" @click="handleViewRelatedApplication(scope.row)" v-if="scope.row.relatedApplication">{{ scope.row.relatedApplication }}</el-link>
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="type"
          label="入库类别"
          width="120"
        />
        <el-table-column
          prop="manager"
          label="入库库管"
          width="100"
        />
        <el-table-column
          prop="confirmTime"
          label="确认时间"
          width="120"
        />
        <el-table-column
          prop="expectedQuantity"
          label="应入库总量"
          width="100"
        />
        <el-table-column
          prop="actualQuantity"
          label="确认入库总量"
          width="120"
        />
      </el-table>
    </div>

    <!-- 分页区域 -->
    <div class="pagination-container">
      <el-pagination
        v-model:current-page="pagination.currentPage"
        v-model:page-size="pagination.pageSize"
        :page-sizes="pagination.pageSizes"
        :total="pagination.total"
        :layout="pagination.layout"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
      <div class="pagination-info">
        显示 {{ (pagination.currentPage - 1) * pagination.pageSize + 1 }} - 
        {{ Math.min(pagination.currentPage * pagination.pageSize, pagination.total) }} 条，
        共 {{ pagination.total }} 条
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { Search, ArrowDown } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import { inboundAPI } from '@/api/modules/inbound'
import { ElMessage } from 'element-plus'

// 获取路由实例
const router = useRouter()

// 搜索表单数据
const searchForm = reactive({
  startDate: '',
  endDate: '',
  type: '',
  code: '',
  subject: '',
  manager: ''
})

// 表格数据
const tableData = ref([])
const loading = ref(false)

// 分页配置
const pagination = reactive({
  currentPage: 1,
  pageSize: 50,
  total: 55,
  layout: "total, sizes, prev, pager, next, jumper",
  pageSizes: [50, 100, 200, 500]
})

// 选中的行
const selectedRows = ref([])

// 处理表格选择变化
const handleSelectionChange = (selection) => {
  selectedRows.value = selection
}

// 处理搜索
const handleSearch = () => {
  pagination.currentPage = 1
  fetchInboundList()
}

// 处理分页大小变化
const handleSizeChange = (val) => {
  pagination.pageSize = val
  fetchInboundList()
}

// 处理页码变化
const handleCurrentChange = (val) => {
  pagination.currentPage = val
  fetchInboundList()
}

// 功能按钮处理方法
const handleAdd = () => {
  console.log('新增入库确认')
  // 跳转到入库确认新增页面
  router.push('/warehouse/inbound-confirmation-new')
}

const handleDelete = () => {
  if (selectedRows.value.length === 0) {
    ElMessage.warning('请先选择要删除的记录')
    return
  }
  console.log('删除选中的记录', selectedRows.value)
}

const handleFilter = () => {
  console.log('过滤数据')
}

const handleRestore = () => {
  console.log('还原数据')
}

const handleImport = () => {
  console.log('导入数据')
}

const handleExport = () => {
  console.log('导出数据')
}

const handleViewLog = () => {
  console.log('查看日志')
}

const handleDesign = () => {
  console.log('设计页面')
}

const handleViewDetail = (row) => {
  console.log('查看入库确认主题详情', row)
  // 跳转到详情页面并携带当前行数据
  router.push({
    path: '/warehouse/inbound-detail',
    query: { 
      id: row.id || row.code,
      // 将行数据序列化为JSON字符串传递
      rowData: JSON.stringify({
        code: row.code,
        subject: row.subject,
        type: row.type,
        manager: row.manager,
        confirmTime: row.confirmTime,
        relatedApplication: row.relatedApplication,
        expectedQuantity: row.expectedQuantity,
        actualQuantity: row.actualQuantity
      })
    }
  })
}

const handleViewInboundDetail = (row) => {
  console.log('查看入库编号详情', row)
  // 跳转到详情页面并携带当前行数据
  router.push({
    path: '/warehouse/inbound-detail',
    query: { 
      id: row.code,
      // 将行数据序列化为JSON字符串传递
      rowData: JSON.stringify({
        code: row.code,
        subject: row.subject,
        type: row.type,
        manager: row.manager,
        confirmTime: row.confirmTime,
        relatedApplication: row.relatedApplication,
        expectedQuantity: row.expectedQuantity,
        actualQuantity: row.actualQuantity
      })
    }
  })
}

const handleViewRelatedApplication = (row) => {
  console.log('查看关联入库申请单详情', row)
  // 跳转到关联申请单模式的入库确认新增页面
  router.push({
    path: '/warehouse/inbound-confirmation-new',
    query: { 
      relatedApplicationId: row.relatedApplicationId || row.code, 
      relatedApplication: row.relatedApplication 
    }
  })
}

// 获取入库确认列表
const fetchInboundList = async () => {
  try {
    loading.value = true
    const params = {
      page: pagination.currentPage,
      pageSize: pagination.pageSize,
      ...searchForm
    }
    const { data } = await inboundAPI.getList(params)
    console.log('入库确认列表数据', data)
    tableData.value = data.items
    pagination.total = data.total
  } catch (error) {
    ElMessage.error('获取入库确认列表失败')
    console.error(error)
  } finally {
    loading.value = false
  }
}

// 初始化
onMounted(() => {
  fetchInboundList()
})
</script>

<style scoped>
.inbound-confirmation {
  padding: 20px;
  background-color: #fff;
  min-height: 100vh;
}

.page-title {
  margin-bottom: 20px;
}

.page-title h2 {
  font-size: 20px;
  font-weight: 500;
  color: #303133;
  margin: 0;
  padding: 0;
}

.operation-bar {
  margin-bottom: 20px;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  border-bottom: 1px solid #ebeef5;
  padding-bottom: 15px;
}

.flex-spacer {
  flex-grow: 1;
}

.search-form {
  margin-bottom: 20px;
  padding: 15px;
  background-color: #f5f7fa;
  border-radius: 4px;
}

.date-separator {
  margin: 0 5px;
  color: #606266;
}

.table-container {
  margin-bottom: 20px;
  border: 1px solid #ebeef5;
  border-radius: 4px;
}

.pagination-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
  padding: 10px 0;
}

.pagination-info {
  color: #606266;
  font-size: 14px;
}

:deep(.el-table) {
  --el-table-header-bg-color: #f5f7fa;
}

:deep(.el-table th) {
  background-color: #f5f7fa;
  color: #606266;
  font-weight: 500;
}

:deep(.el-form-item) {
  margin-bottom: 18px;
  margin-right: 20px;
}

:deep(.el-form-item__label) {
  color: #606266;
}

:deep(.el-table .cell) {
  padding: 8px 12px;
}

:deep(.el-table--enable-row-hover .el-table__body tr:hover > td) {
  background-color: #f5f7fa;
}

:deep(.el-button) {
  font-weight: 400;
}

:deep(.el-table__row) {
  height: 40px;
}

:deep(.el-table__header) {
  height: 40px;
}
</style>